package com.wantong.jianpu.customviews;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;

import com.wantong.jianpu.R;

import me.goldze.mvvmhabit.utils.StringUtils;

/**
 * @author PACY
 * @date 2022/4/25
 * description:
 */
public class CommSelectView extends LinearLayout {
    String title;
    String content;
    String hintText;
    boolean showLine;
    boolean selectPoint;

    TextView titleTv;
    TextView contentTv;
    ImageView labelIv;
    TextView lineTv;
    TextView pointTv;

    boolean showLabelIcon;

    public CommSelectView(Context context) {
        super(context);
    }

    public CommSelectView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        /**
         * 加载布局  获取自定义属性
         */
        LayoutInflater.from(context).inflate(R.layout.comm_select_view, this, true);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CommSelectView);
        title = a.getString(R.styleable.CommSelectView_selectTitle);
        content = a.getString(R.styleable.CommSelectView_selectContent);
        hintText = a.getString(R.styleable.CommSelectView_selectHint);
        showLabelIcon = a.getBoolean(R.styleable.CommSelectView_showLabelIcon, true);
        showLine = a.getBoolean(R.styleable.CommSelectView_selectShowLine, true);
        selectPoint=a.getBoolean(R.styleable.CommSelectView_selectPoint, false);
        a.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        contentTv = findViewById(R.id.content);
        titleTv = findViewById(R.id.title);
        labelIv = findViewById(R.id.label_iv);
        lineTv = findViewById(R.id.line_tv);
        pointTv=findViewById(R.id.point_tv);
        setTitleTv(title);
        if (showLabelIcon) {
            labelIv.setVisibility(VISIBLE);
        } else {
            labelIv.setVisibility(GONE);
        }

        if (selectPoint) {
            pointTv.setVisibility(VISIBLE);
        } else {
            pointTv.setVisibility(INVISIBLE);
        }
        if (showLine) {
            lineTv.setVisibility(VISIBLE);
        } else {
            lineTv.setVisibility(GONE);
        }
        if (StringUtils.isNotEmpty(content)) {
            setContent(content);
        }
        if (StringUtils.isNotEmpty(hintText)) {
            setHintText(hintText);
        }
    }

    /**
     * 设置内容
     */
    public void setContent(String content) {
        contentTv.setText(content);

    }

    /**
     * 设置hint
     */
    public void setHintText(String hintText) {
        contentTv.setHint(hintText);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitleTv(String title) {
        titleTv.setText(title);
    }


    @BindingAdapter("select_content")
    public static void setStrContent(CommSelectView commSelectView, String content) {
        if (commSelectView != null) {
            String mCurrentStr = commSelectView.contentTv.getText().toString().trim();
            if (StringUtils.isNotEmpty(content)) {
                if (!content.equalsIgnoreCase(mCurrentStr)) {
                    commSelectView.contentTv.setText(content);
                }
            }
        }
    }
}
